package com.atgouwu.mall.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atgouwu.mall.auth.feign.MemberFeignService;
import com.atgouwu.mall.auth.vo.UserLoginVo;
import com.atgouwu.mall.common.utils.JwtUtils;
import com.atgouwu.mall.common.utils.R;
import com.atgouwu.mall.common.vo.MemberResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

import static com.atgouwu.mall.common.constant.AuthServerConstant.LOGIN_USER;

/**
 * 账号密码登录
 */
@Api(tags = "登录")
@RestController
@RequestMapping("auth/login")
public class LoginController {

    @Autowired
    private MemberFeignService memberFeignService;

//    //登录页modeAndView(前后端不分离)
//    @GetMapping(value = "/login.html")
//    public String loginPage(HttpSession session) {
//        Object attribute = session.getAttribute(LOGIN_USER); //从session取出用户信息,判断是否登录了
//        if (attribute == null) { //用户没登录跳转到登录页面
//            return "login";
//        } else {
//            return "redirect:http://gulimall.com"; //已登录重定向到首页
//        }
//    }
//
//    //账号密码登录(前后端不分离)
//    @PostMapping(value = "/login")
//    public String login(UserLoginVo vo, RedirectAttributes attributes, HttpSession session) {
//        R login = memberFeignService.login(vo); //登录(远程调用member服务)
//        if (login.getCode() == 0) {
//            MemberResponseVo data = login.getData("data", new TypeReference<MemberResponseVo>() {
//            });
//            session.setAttribute(LOGIN_USER, data); //通过springsession将数据存入redis
//            return "redirect:http://gulimall.com"; //重定向到首页
//        } else {
//            Map<String, String> errors = new HashMap<>();
//            errors.put("msg", login.getData("msg", new TypeReference<String>() {
//            }));
//            attributes.addFlashAttribute("errors", errors);
//            return "redirect:http://auth.gulimall.com/login.html";
//        }
//    }
//
//    //退出(前后端不分离)
//    @GetMapping(value = "/logout.html")
//    public String logout(HttpServletRequest request) {
//        request.getSession().removeAttribute(LOGIN_USER); //删除session中用户信息
//        request.getSession().invalidate();
//        return "redirect:http://gulimall.com"; //重定向到首页
//    }

    //账号密码登录(前后端分离)
    @ApiOperation(value = "账号密码登录")
    @PostMapping(value = "/api/login")
    public R login(@RequestBody UserLoginVo vo) {
        R login = memberFeignService.login(vo); //登录(远程调用member会员服务)
        if (login.getCode() == 200) {
            MemberResponseVo data = login.getData("data", new TypeReference<MemberResponseVo>() {
            });
            data.setToken(JwtUtils.createToken(data.getId(), data.getUsername()));
            return R.ok().put("data", JSONObject.toJSONString(data));
        } else {
            return R.error(login.getData("msg", new TypeReference<String>() {
            }));
        }
    }

    //退出登录(前后端分离)
    @ApiOperation(value = "退出登录")
    @GetMapping(value = "/api/logout")
    public R logout(HttpServletRequest request) {
        //request.getSession().removeAttribute(LOGIN_USER); //删除session中用户信息
        //request.getSession().invalidate();
        JwtUtils.removeToken(""); //删除token
        return R.ok();
    }

}
